//
//var gcvs = document.getElementById('view');
//var gctx = gcvs.getContext("2d");
//// 画布的宽度和长度
//var g_width = parseInt(gcvs.getAttribute("width"));
//var g_height = parseInt(gcvs.getAttribute("height"));

window.onload = function()
{
	// 192.168.1.114
	// 192.168.1.103
	// 132.97.249.67
	// 113.64.195.236
	//
	game.init();
	game.index();
	game.connect("127.0.0.1", "8555");
}

var game =
{
	cvs : null,
	ctx : null,
	cvs_w : null,
	cvs_h : null,
	websocket : null,
	user : null,
	player : null,
	// 目前只有一个hall(以后扩展,总之*最外层*是单个对象)
	hall : null,

	init : function()
	{
		this.cvs = document.getElementById('view');
		// 卧底一个this
		this.cvs.game = this;
		this.ctx = document.getElementById("view").getContext("2d");
		this.cvs_w = parseInt(this.cvs.getAttribute("width"));
		this.cvs_h = parseInt(this.cvs.getAttribute("height"));
		this.user = new User_Normal();
		
//		var d = new Image();
//		d.src = "media/images/ui/hall/user_list_1.jpg";
//		
//		console.log(d.width);
		
	},
	
	connect : function(host, port)
	{
		this.websocket = new WebSocket('ws://' + host + ':' + port);
		// 卧底一个this
		this.websocket.game = this;
		this.websocket.onmessage = function(msg)
		{
			var _ws = this;
			this.game.onRecv(msg, _ws);
		};
		this.websocket.onopen = this.onOpen;
	},
	
	index : function()
	{
		var _img = new Image();
		var x = 390;
		var y = 300;
		var w = 425;
		var h = 215;
		_img.src = "media/images/ui/login/logo.png";
		_img.onload = function()
		{
			game.ctx.drawImage(_img, x, y, w, h);
		}
		
	},
	
	/**
	 * 清屏
	 */
	clean : function()
	{
		this.ctx.clearRect(0, 0, this.cvs_w, this.cvs_h);
	},

	login : function(a_username, a_password)
	{
//		var pro = new Object();
//		pro.user_nickname = "kk";
//		pro.user_level = 123;
//		pro.user_name = "o010101";
//		pro.drawable_playeravatar = "/dhdhe/fdf";
//		console.log(pro);
//		
//		var pro1 = new Object();
//		pro1.user_nickname = "kk23";
//		pro1.user_level = 113;
//		pro1.user_name = "oddfd";
//		pro1.drawable_playeravatar = "/333/111";
//		pro1.prototype = new User_Base();
//		pro1.prototype.constructor = User_Base;
//		console.log(pro1);
//		
//		var pro2 = new User_Base();
//		console.log(pro2);
		
		var _pro_login = new Pro_Login(a_username, a_password);
		this.send(_pro_login);
	},

	temp_INHALL : function()
	{
		var _pro_joinhall = new Pro_JoinHall(1);
		this.send(_pro_joinhall);
	},

	// 初始化大厅的数据
	joinHall : function(a_pro)
	{
		this.hall = new Hall_Normal(1, "1");
		this.hall.init(this, a_pro);
		// this.cvs.addEventListener("mousemove", this.onHallMove, false);
	},

	// ///////////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////////
	// ///////////////////////////////////////////////////////////////

	send : function(data)
	{
		var _data = JSON.stringify(data);
//		alert(_data);
		this.websocket.send(_data);
	},

	onOpen : function(msg)
	{
//		alert("open");
	},

	onRecv : function(msg, attach)
	{
		alert("on Global Recv:" + msg.data);
		var parsed = JSON.parse(msg.data);
//		parsed.user.__proto__ = User_Base.prototype;
//		
//		console.log(parsed);
		switch (parsed.pro_id)
		{
		// 登录
		case '502':
			this.user.parseFromPro(parsed.user);
			// 登陆后在选大厅界面,也需要用户头像位置
			this.user.position = new Position_Normal(710, 20, 145, 150);
			alert("登陆成功：" + this.user.user_nickname);
			
			this.clean();
			
			this.user.drawSelf();
			
			this.ctx.fillStyle = "#cecece";
			this.ctx.font = '12pt Calibri';   
			this.ctx.fillText("昵称         " + this.user.user_nickname, 720, 210);
			this.ctx.fillText("用户名    " + this.user.user_name, 720, 240);
			
			break;
		// 进入大厅
		case '852':
			this.user.cleanSelf();
			this.joinHall(parsed["hall"]);
			break;
		case 'error':
			alert("error");
			break;
		default:
			throw new Error('Unknown message type ' + parsed.type);
			break;
		}
	}
};

